package ink.teamwork.stock.service.impl;

import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;
import ink.teamwork.stock.dao.OptionalDAO;
import ink.teamwork.stock.entity.Optional;
import ink.teamwork.stock.service.OptionalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
@Transactional
public class OptionalServiceImpl implements OptionalService {

    @Autowired
    private OptionalDAO optionalDAO;

    public boolean save(Optional optional) {
        return optionalDAO.save(optional);
    }

    public boolean update(Optional optional) {
        try {
            optionalDAO.save(optional);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public SearchResult<Optional> queryPageByUserId(int start, int size, String userId) {
        Search search = new Search();
        search.addFilterEqual("userId", userId);
        search.setFirstResult(start);
        search.setMaxResults(size);
        return optionalDAO.searchAndCount(search);
    }
}
